package com.wwwarehouse.contract.release_supply_requirements;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.contract.bean.LocalPublisgBean;
import com.wwwarehouse.contract.bean.SelectResourseBean;
import com.wwwarehouse.contract.common.ContractCommon;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/25 15:18
 * @描述 ${TODO}
 */
@Route(path = "/contract/LocalPublishResourceListFragment")
public class LocalPublishResourceListFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private CustomViewPagerInternal mVpContain;
    private CirclePageIndicator mPageIndicate;
    private Button mBtSure;
    private List<Fragment> mFragmentList;
    private SlideBarView mSlideBarView;
    private String mBuId;
    private List<SelectResourseBean.ListBean> mSelectToCanSeePublishList;
    private List<LocalPublisgBean> mLocalPublishList;
    private Bundle mBundleAsk;
    private LocalPublishResourceListFragmentChild mLocalPublishResourceListFragmentChild;
    private Map mMapLocalPublish;
    private long mLongBuId;
    private Bundle mPublishNumBundle;

    private Bundle mSelectToCanSeePublishBundle;
    private List<SelectResourseBean.ListBean> mForLocalPublishList;
    private RelativeLayout mRl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_local_resourse_list, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    public void onEventMainThread(String string) {
        if ("ForLocalPublishListSize".equals(string)) {
            if (ContractCommon.getInstance().getForLocalPublishList().size() == 0) {
                mRl.setVisibility(View.INVISIBLE);
//                mBtSure.setEnabled(false);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().post("LOCAL_BACK_SELECT_ADAPTER");//返回选择页面刷新adapter
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        mVpContain = findView(mView, R.id.cvpi);
        mPageIndicate = findView(mView, R.id.cpi);
        mSlideBarView = findView(mView, R.id.sbv_slide);
        mRl = findView(mView, R.id.rl);
        mBtSure = findView(mView, R.id.btn_sure);
        mBtSure.setOnClickListener(this);

    }

    private void initData() {
        mForLocalPublishList = new ArrayList<>();
        ContractCommon.getInstance().setForLocalPublishList(mForLocalPublishList);
        mLocalPublishList = new ArrayList<>();//发布接口用的集合  只用了存储起来的集合的部分参数

        mFragmentList = new ArrayList<>();
        mSelectToCanSeePublishList = ContractCommon.getInstance().getSelectToCanSeePublishList();//选中的但未发布的集合
        LogUtils.showLog("此时选择单位发布的集合大小" + mSelectToCanSeePublishList.size());
        ContractCommon.getInstance().getForLocalPublishList().clear();//清空一次
        for (int i = 0; i < mSelectToCanSeePublishList.size(); i++) {
            if (!ContractCommon.getInstance().getForLocalPublishList().contains(mSelectToCanSeePublishList.get(i))) {
                ContractCommon.getInstance().getForLocalPublishList().add(mSelectToCanSeePublishList.get(i));//将选中的集合全部add零时集合
            }
        }
        //        ContractCommon.getInstance().getForLocalPublishList().addAll(mSelectToCanSeePublishList);
        mSelectToCanSeePublishBundle = getArguments();
        if (mSelectToCanSeePublishBundle != null) {

            mBuId = mSelectToCanSeePublishBundle.getString("buId");
            mLongBuId = Long.parseLong(mBuId);
        }
        mMapLocalPublish = new HashMap();
        mPublishNumBundle = new Bundle();//总共发布多少条


    }

    private void initEvent() {
        int size = mSelectToCanSeePublishList.size();
        final int page = (size % 7 == 0) ? size / 7 : (size / 7 + 1);
        mFragmentList.clear();
        for (int j = 0; j < page; j++) {
            mBundleAsk = new Bundle();
            newFragment(j);//创建子fragment
            mLocalPublishResourceListFragmentChild.setArguments(mBundleAsk);
            mFragmentList.add(mLocalPublishResourceListFragmentChild);
        }

        mVpContain.setAdapter(new ContractFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));
        if (page <= 6) {
            mPageIndicate.setVisibility(View.VISIBLE);
            mSlideBarView.setVisibility(View.GONE);
            mPageIndicate.setViewPager(mVpContain, 0);
        } else {
            mPageIndicate.setVisibility(View.GONE);
            mSlideBarView.setVisibility(View.VISIBLE);
            mSlideBarView.setTotalPage(page);
            mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                @Override
                public void onSlideChange(int i) {
                    mVpContain.setCurrentItem(page, false);
                }
            });
            mSlideBarView.setViewPagerInternal(mVpContain);
        }
    }

    private void newFragment(int j) {
        mLocalPublishResourceListFragmentChild = new LocalPublishResourceListFragmentChild();
        mBundleAsk.putInt("page", j + 1);
        //        mBundleAsk.putSerializable("mSelectResourseBean", (Serializable) mSelectToCanSeePublishList);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof LocalPublishResourceListFragment)
            mActivity.setTitle(mActivity.getResources().getString(R.string.publish_list));
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {

        for (int i = 0; i < ContractCommon.getInstance().getForLocalPublishList().size(); i++) {

            LocalPublisgBean localPublisgBean = new LocalPublisgBean();
            localPublisgBean.setRsDefinedUkid(ContractCommon.getInstance().getForLocalPublishList().get(i).getDefinedUkid());
            localPublisgBean.setRsImg(ContractCommon.getInstance().getForLocalPublishList().get(i).getRelatedUrl());
            localPublisgBean.setRsName(ContractCommon.getInstance().getForLocalPublishList().get(i).getDefinedName());
            mLocalPublishList.add(localPublisgBean);

        }
        mMapLocalPublish.put("buId", mLongBuId);
        mMapLocalPublish.put("tempRsItems", mLocalPublishList);


        mPublishNumBundle.putInt("PublishNum", mLocalPublishList.size());
        mPublishNumBundle.putLong("buId", mLongBuId);
        NoHttpUtils.httpPost(ContractConstant.SAVE, mMapLocalPublish, mOnResponseListener, 0);
        showProgressDialog(mActivity.getString(R.string.submiting));

    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                if ("0".equals(commonClass.getCode())) {
                    popFragment();
                    pushFragment("/contract/PublishSuccessFragment", mPublishNumBundle);
                } else {
                    toast(commonClass.getMsg());
                }
            } catch (Exception e) {
                LogUtils.showLog("Exception" + e.toString());
            }

        }

        @Override
        public void onFailed(String s, int i) {
            toast(s);
        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };


}
